package com.xcity.db.entity;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.xcity.db.mapper.Relation2Mapper;
import com.xcity.game.relation.RelationPool;

/**
 * 玩家之间的关系
 * @author yang.li
 *
 */
@SuppressWarnings("serial")
public class Relation2 extends AbstractEntity<Relation2Mapper> {

	protected int id; // auto increment
	protected byte type;
	protected long srcId;
	protected long destId;
	protected int intimacy; // 亲密度
	protected Date time;
	protected RelationPool pool;

	// 关系类型(好友、仇人)
	public static final byte TYPE_FRIEND = 0, TYPE_ENEMY = 1;
	
	public static final int MAX_INTIMACY = 500;
	public static final int HELP_FRIEND_ADD_INTIMACY = 2; // 帮助好友收钱增加2点亲密度
	public static final int NO_LOGIN_DEC_INTIMACY = 10; // 其中一方在n天内未登录则每天掉10点亲密度
	public static final int CHECK_NO_LOGIN_DAYS = 15; // 掉亲密度未登录天数
	public static final int TAP_LIKE_ADD_INTIMACY = 50; // 好友之间点赞增加50点亲密度
	
	protected static final Logger LOG = LoggerFactory.getLogger(Relation2.class);
	
	public Relation2() {
		this.state = EntityState.UPDATE;
	}
	
	public Relation2(Player src, Player dest, byte type) {
		this(src.getId(), dest.getId(), type);
	}
	
	public Relation2(long srcId, long destId, byte type) {
		this.state = EntityState.INSERT;
		this.srcId = srcId;
		this.destId = destId;
		this.type = type;
		this.time = new Date();
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public byte getType() {
		return type;
	}

	public void setType(byte type) {
		this.type = type;
	}

	public long getSrcId() {
		return srcId;
	}

	public void setSrcId(long srcId) {
		this.srcId = srcId;
	}

	public long getDestId() {
		return destId;
	}

	public void setDestId(long destId) {
		this.destId = destId;
	}

	public int getIntimacy() {
		return intimacy;
	}

	public void setIntimacy(int intimacy) {
		this.intimacy = intimacy;
	}
	
	public Date getTime() {
		return time;
	}

	public void setTime(Date time) {
		this.time = time;
	}
	
	public RelationPool getPool() {
		return pool != null ? pool : (pool = new RelationPool());
	}

	public void setPool(RelationPool pool) {
		this.pool = pool;
	}

	public boolean addIntimacy(int val, String cause) {
		if (val <= 0 || intimacy >= MAX_INTIMACY) {
			return false;
		}
		int oldVal = intimacy;
		intimacy += val;
		if (intimacy > MAX_INTIMACY) {
			intimacy = MAX_INTIMACY;
		}
		LOG.info("[ADD FRIEND INTIMACY]SRC[{}]DEST[{}]OLD[{}]NEW[{}]CAUSE[{}]", srcId, destId, oldVal, intimacy, cause);
		return true;
	}
	
	public boolean decIntimacy(int val, String cause) {
		if (val <= 0 || intimacy < val) {
			return false;
		}
		int oldVal = intimacy;
		intimacy -= val;
		LOG.info("[DEC FRIEND INTIMACY]SRC[{}]DEST[{}]OLD[{}]NEW[{}]CAUSE[{}]", srcId, destId, oldVal, intimacy, cause);
		return true;
	}
	
}
